// pages/classify/index.js
import IndexModel from '../../models/IndexModel';
const indexModel = new IndexModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectData: [],
    ageData: [],
    companyList: [],
    type: '',
    form: '',
    dev: '',
    age: '',
    page: 1,
    page_total: 0,
  },

  selListener(e) {
    const {
      currentTarget: { dataset },
    } = e;
    const label = JSON.parse(JSON.stringify(this.data.ageData));
    let ageLabel = [];
    label.forEach(el => {
      if (el.id === dataset.param.id) {
        el.isSel = !el.isSel;
      }
      if (el.isSel) {
        ageLabel.push(el.fenlei)
      }
    });
    this.setData({
      ageData: label,
      age: ageLabel.join(','),
    })
    let {
      page,
      type: proType,
      form,
      dev,
      age
    } = this.data;
    this.fetchCompanyList(page, true, proType, form, dev, age)
  },

  checkboxChange(e) {
    // console.log(e);
    let {
      type,
      value,
    } = e.detail;
    switch (type) {
      case 'chanpinfeilei':
        this.setData({
          type: value.join(','),
        })
        break;
      case 'shangyemoshifenlei':
        this.setData({
          form: value.join(','),
        })
        break;
      case 'fazhanfangxiang':
        this.setData({
          dev: value.join(','),
        })
        break;
    }
    let { page, type: proType, form, dev, age } = this.data;
    this.fetchCompanyList(page, true, proType, form, dev, age)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    Promise.all([
      indexModel.getIndexClassify(),
      indexModel.getClassifyOption('nianlingfanwei'),
      this.fetchCompanyList(1, true),
    ]).then(res => {
      let classifyType = res[0].JSONdata.List0;
      let content = res[0].JSONdata.List1[0];
      let selectData = this.handleClassifyList(classifyType, content);
      // console.log('select data->', selectData);
      let ageLabel = res[1].ListJson.List;
      ageLabel.forEach((item, i) => {
        item.id = '10' + i;
        item.isSel = false;
      })
      this.setData({
        selectData,
        ageData: ageLabel,
      })
    })
  },

  fetchCompanyList(page, override, type, form, dev, age) {
    wx.showLoading({
      title: '加载中',
    })
    this.loading = true;
    indexModel.queryCompanyInfo(page, type, form, dev, age).then(res => {
      const list = res.ListJson.List;
      const page_total = res.ListJson.Result.TotalPageCount;
      this.setData({
        page,
        page_total: Number(page_total),
        companyList: override ? list : this.data.companyList.concat(list),
      })
      wx.hideLoading();
      this.loading = false;
    }).catch(e => {
      console.error(e)
      this.loading = false;
    })
  },

  handleClassifyList(classifyType, content) {
    classifyType.forEach(item => {
      const key = item.jyCompanyziduan;
      if (content[key]) {
        item.value = content[key]
      }
    })

    return classifyType;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.loading && this.data.page < this.data.page_total) {
      this.fetchCompanyList(this.data.page + 1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})